﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerShipMethodDao : IShipMethodDao
    {
        public List<ShipMethod> GetShipMethods()
        {
            string procedures = "Get_All_ShipMethods";

            return Db.ReadList(procedures, true, Make, null);
        }

        public ShipMethod GetShipMethod(string shipMethodId)
        {
            string procedures = "Get_ShipMethods_ByCondition";

            object[] parms = { "@WhereClause", "WHERE ShipMethodID = '" + shipMethodId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_ShipMethod";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public List<ShipMethod> GetShipMethodsPaged(int currentPage)
        {
            string procedures = "Get_Paged_ShipMethods";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<ShipMethod> GetShipMethodsActive(bool isActive)
        {
            string procedures = "Get_ShipMethods_ByCondition";
            int _isActive = 0;

            if (isActive == true) { _isActive = 1; }

            object[] parms = { "@WhereClause", " WHERE IsActive = " + _isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertShipMethod(ShipMethod shipMethod)
        {
            string sql = "Insert_ShipMethod";

            shipMethod.ShipCode = Db.Insert(sql, true, Take(shipMethod));
        }

        public void UpdateShipMethod(ShipMethod shipMethod)
        {
            string sql = "Update_ShipMethod";

            Db.Update(sql, true, Take(shipMethod));
        }

        public void DeleteShipMethod(ShipMethod shipMethod)
        {
            string sql = "Delete_ShipMethod";

            Db.Update(sql, true, Take(shipMethod.ShipMethodID));
        }

        private object[] Take(string shipMethodId)
        {
            return new object[]  
            {
                "@ShipMethodID", shipMethodId,
            };
        }

        private object[] Take(ShipMethod shipMethod)
        {
            return new object[]  
            {
                "@ShipMethodID", shipMethod.ShipMethodID,
                "@ShipCode", shipMethod.ShipCode,
                "@ShipName", shipMethod.ShipName,
                "@Description", shipMethod.Description,
                "@IsActive", shipMethod.IsActive
            };
        }

        private static Func<IDataReader, ShipMethod> Make = reader =>
            new ShipMethod
            {
                ShipMethodID = reader["ShipMethodID"].AsString(),
                ShipCode = reader["ShipCode"].AsString(),
                ShipName = reader["ShipName"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
